<!--
* 任务界面
*
* @author lotswn
* @version 1.0
-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>检测管理</title>
    <link rel="stylesheet" href="../component/pear/css/pear.css" />
    <style>
        .expand.pear-btn:hover {
            color: currentColor;
            background-color: unset;
        }
        .expand.pear-btn {
            border: 1px solid rgb(255 255 255 / 0%)
        }
    </style>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="userForm" class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" >任务名</label>
                    <div class="layui-input-inline">
                        <input type="text" id = "searchName" placeholder="请输入任务名" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" >主机编号</label>
                    <div class="layui-input-inline">
                        <input type="text" id = "searchHost" placeholder="请输入主机编号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label"  >用户编号</label>
                    <div class="layui-input-inline">
                        <input type="text" id = "searchUser" placeholder="请输入用户编号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label"  >任务状态</label>
                    <div class="layui-input-inline">
                        <select name="status" id = "searchStatus" value="">
                            <option value="">请选择任务状态</option>
                            <option value="0">待检测</option>
                            <option value="1">已完成</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: 50px;">
                    <a class="pear-btn pear-btn-md pear-btn-primary"  id = "searchVal">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </a>
                    <a type="reset" class="pear-btn pear-btn-md" id = "iReset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </a>
                    <button type="button" class="pear-btn pear-btn-md expand">
                    </button>
                </div>

            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table class = "layui-hide" id="task-table" lay-filter="task-table"></table>
    </div>
</div>

<!--头部工具栏-->
<script type="text/html" id="task-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchDelete">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<!--行工具栏-->
<script type="text/html" id="task-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="look"><i
            class="layui-icon layui-icon-about"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del"><i
            class="layui-icon layui-icon-delete"></i></button>
    <!--    <button class="pear-btn pear-btn-sm" id="more_{{d.userId}}"><i class="layui-icon layui-icon-triangle-d"></i></button>-->
</script>


<div class="layui-card" style="display: none;" id="task_report">
        <div class="layui-card-header">检 测 结 果</div>
        <div class="layui-card-body">
            <div class="layui-tab layui-tab-card">
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <table style="margin-top: 10px;" id="report-table" lay-filter="report-table"></table>
                    </div>
                </div>
            </div>
    </div>
</div>

<script src="../component/layui/layui.js"></script>
<script src="../component/pear/pear.js"></script>
<script src="../component/jquery/jquery-3.6.0.js" charset="utf-8"></script>

<script>
    $("#iReset").click(function () {
        $("#searchName").val("");
        $("#searchHost").val("");
        $("#searchUser").val("");
        $("#searchStatus").val("");
        layui.form.render('select');
        tableFlush();
    });

    $("#searchVal").click(function () {
        let searchName = $("#searchName").val();
        let searchHost = $("#searchHost").val();
        let searchUser = $("#searchUser").val();
        let searchStatus = $("#searchStatus").val();
        tableFlush(searchName, searchHost, searchUser, searchStatus);
    });


    function tableFlush(searchName,searchHost,searchUser,searchStatus) {
        $("#task_report").css("display", "none")
        let search_json = {};
        if(searchName != null && searchName != '') {
            search_json.taskname = searchName;
        }
        if(searchHost != null && searchHost != '') {
            search_json.hostid = searchHost;
        }
        if(searchUser != null && searchUser != '') {
            search_json.userid = searchUser;
        }
        if(searchStatus != null && searchStatus != '') {
            search_json.status = searchStatus;
        }
        layui.use(['table', 'drawer','form'], function(){
            var table = layui.table;
            let drawer = layui.drawer;
            let form = layui.form;

            table.render({
                elem: '#task-table'
                ,url: '/task/selectByPage'
                ,where: search_json                // 追加搜索条件
                ,toolbar: '#task-toolbar' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '任务数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{fixed: 'left', field:'id', title:'ID', width:80, unresize: true, templet: function(res){
                            return '<em>'+ res.id +'</em>'
                        }}
                    ,{field:'taskname', title:'任务名', width:150, templet: function(res){
                            return '<em>'+ res.taskname +'</em>'
                        }}
                    ,{field:'hostid', title:'主机编号', width:120, templet: function(res){
                            return '<em>'+ res.hostid +'</em>'
                        }}
                    ,{field:'userid', title:'用户编号', width:120, templet: function(res){
                            return '<em>'+ res.userid +'</em>'
                        }}
                    ,{field:'hostinfo', title:'主机信息', width:100, templet: function(res){
                            if(res.hostinfo === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'gpedit', title:'安全策略', width:100, templet: function(res){
                            if(res.gpedit === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'patchinfo', title:'补丁信息', width:100, templet: function(res){
                            if(res.patchinfo === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'netinfo', title:'网络信息', width:100, templet: function(res){
                            if(res.netinfo === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'backdoor', title:'后门', width:100, templet: function(res){
                            if(res.backdoor === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'selfstart', title:'自启动项', width:100, templet: function(res){
                            if(res.selfstart === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'service', title:'服务项', width:100, templet: function(res){
                            if(res.service === 1) {return '<em>'+ "核查" +'</em>';}
                            else    return '<em>'+ "未核查" +'</em>';
                        }}
                    ,{field:'createtime', title:'创建日期', width:120, templet: function(res){
                            return '<em>'+ layui.util.toDateString(res.create_time, 'yyyy-MM-dd') +'</em>'
                        }}
                    ,{field:'status', title:'任务状态', width:100, templet: function(res){
                            if(res.status === 1) {return '<em>'+ "已完成" +'</em>';}
                            else    return '<em>'+ "待检测" +'</em>';
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#task-bar', width:150}
                ]]
                ,page: true
            });

            //监听行工具事件
            table.on('tool(task-table)', function(obj) {
                if (obj.event === 'del') {
                    window.delete(obj);
                } else if (obj.event === 'look') {
                    window.look(obj);
                }
            });

            //监听头工具事件
            table.on('toolbar(task-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchDelete') {
                    window.batchDelete(obj);
                }
            });


            window.delete = function(obj) {
                var data = obj.data;
                layer.confirm('确认删除所选任务', function(index){
                    $.ajax({
                        url:"/task/deleteById",       // 请求路径
                        type:"post",                  // 请求的方式，不区分大小写
                        cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                        data: {
                            id:data.id
                        },
                        datatype:"json",             // 返回类型，text文本、html页面、json数据
                        success:function(response){
                            console.log("返回: " + response);
                            if(response.code === 200) {
                                layer.msg("删除用户成功!");
                                layer.close(index);                         // 关闭当前窗口
                                tableFlush();
                            }
                        },
                        error:function(response){
                            console.log("出错返回: " + response);
                        }
                    });
                });
            }
            window.batchDelete = function (obj){
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].id + ",";
                }
                // ids = ids.substr(0, ids.length - 1);
                layer.confirm('确定要删除这些任务', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url:"/task/batchDeleteByIds",       // 请求路径
                        type:"post",                  // 请求的方式，不区分大小写
                        cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                        data: {
                            ids:ids
                        },
                        datatype:"json",             // 返回类型，text文本、html页面、json数据
                        success:function(response){
                            console.log("返回: " + response);
                            if(response.code === 200) {
                                layer.msg("删除任务成功!");
                                layer.close(index);                         // 关闭当前窗口
                                tableFlush();
                            }
                        },
                        error:function(response){
                            console.log("出错返回: " + response);
                        }
                    })
                });
            }
            window.add = function() {
                layer.open({
                    type: 2,
                    title: '新建任务',
                    shade: 0.1,
                    area: ['500px', '400px'],
                    moveType: 1, //拖拽模式，0或者1
                    content: 'operate/addtask.html'
                });
            }

            window.look = function (obj) {
                search_json.taskid = obj.data.id;
                if(obj.data.status === 0)
                {
                    layer.msg("任务尚未完成!");
                    $("#task_report").css("display", "none")
                }
                else
                {
                    layer.msg("加载成功!");
                    // $("#task_report").css("display", "none")
                    // alert("report.html?taskid="+obj.data.id)
                    // layui.admin.addTab(1,"检测结果","./blview/report.html")
                    // window.location.href("report.html?taskid="+obj.data.id)
                    $("#task_report").css("display", "block")
                    table.render({
                        elem: '#report-table'
                        ,url: '/report/selectByPage'
                        ,where: search_json                // 追加搜索条件
                        ,title: '报告日志'
                        ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                            title: '提示'
                            ,layEvent: 'LAYTABLE_TIPS'
                            ,icon: 'layui-icon-tips'
                        }]
                        ,cols: [[
                            // {align:'center', field:'id', title:'ID', width:80, unresize: true, templet: function(res){
                            //         return '<em>'+ res.id +'</em>'
                            //     }},
                            {align:'center', field:'taskid', title:'任务编号',  templet: function(res){
                                    return '<em>'+ res.taskid +'</em>'
                                }}
                            ,{align:'center',field:'checktype', title:'检测类型',  templet: function(res){
                                    return '<em>'+ res.checktype +'</em>'
                                }}
                            ,{align:'center',field:'indexname', title:'检测条目', templet: function(res){
                                    return '<em>'+ res.indexname +'</em>'
                                }}
                            ,{align:'center',field:'currentval', title:'检测值', templet: function(res){
                                    return '<em>'+ res.currentval +'</em>'
                                }}
                            ,{align:'center',field:'adviseval', title:'修改意见/加固意见',  templet: function(res){
                                    return '<em>'+ res.adviseval +'</em>'
                                }}
                        ]]
                        ,page: true
                    });
                }}
        });
    }
    tableFlush();
</script>
</body>

</html>